//
//  informationVC.m
//  APP_login
//
//  Created by edz on 7/4/21.
//  Copyright © 2021 baicai. All rights reserved.
//

#import "InformationVC.h"
#import "DatePickerView.h"
#import "InformationModel.h"
#import "TabBarViewController.h"
#import "UserSingleton.h"
#import "BZKTools.h"
#import <Masonry.h>

@interface InformationVC ()<UITextFieldDelegate,DatePickerViewDelegate>
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UIButton* nextButton;
@property (nonatomic, strong) UIBarButtonItem *rightItem;
@property (nonatomic, strong) UILabel *infoLable;
@property (nonatomic, strong) UITextField *infoTextField;
@property (nonatomic, strong) UIImageView *iconImage;
@property (nonatomic, strong) UILabel *nickNameLabel;
@property (nonatomic, strong) UITextField *nickNaneTextField;
@property (nonatomic, strong) UIButton * femaleIconButton;
@property (nonatomic, strong) UILabel *femaleLabel;
@property (nonatomic, strong) UIButton *manIconButton;
@property (nonatomic, strong) UILabel *manLabel;
@property (nonatomic, strong) UILabel *birthdayLable;
@property (nonatomic, strong) UIButton *birthdayButton;
@property (nonatomic, strong) NSString *birthdayData;
@property (nonatomic, strong) UILabel *birthdayDataLable;
@property (nonatomic, assign) GenderType sex;

@end

@implementation InformationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    //导航栏
    self.title = @"2/2";
    UIImage *triangleImage = [UIImage imageNamed:@"triangle"];
    _backButton = [UIButton systemButtonWithImage:triangleImage target:self action:@selector(backButtonDidPress)];
    //左按钮
//    _backButton.frame = CGRectMake(0, 0, 10, 10);
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make){
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(30);
    }];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:_backButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    //右按钮
    _nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    _nextButton.frame = CGRectMake(0, 0, 70, 15);
    [_nextButton mas_makeConstraints:^(MASConstraintMaker *make){
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(30);
    }];
    [_nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [_nextButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _nextButton.backgroundColor = [UIColor colorWithRed:239/255 green:241/255 blue:242/255 alpha:0.3];
    _nextButton.titleLabel.textColor = [UIColor whiteColor];
    
    [_nextButton.layer setMasksToBounds:YES];
    [_nextButton.layer setCornerRadius:7.0];
    [_nextButton addTarget:self action:@selector(nextButtonDidPress) forControlEvents:UIControlEventTouchUpInside];
    
    _rightItem = [[UIBarButtonItem alloc] initWithCustomView:_nextButton];
    _rightItem.enabled = YES;
    self.navigationItem.rightBarButtonItem = _rightItem;
    
    //infoLabel
    _infoLable = [[UILabel alloc] init];
    [self.view addSubview:_infoLable];
    [_infoLable mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(40);
        make.width.mas_equalTo(140);
        make.height.mas_equalTo(30);
    }];
    _infoLable.text = @"填写资料";
    _infoLable.font = [UIFont systemFontOfSize:30];
    
    //infoTextField
    _infoTextField = [[UITextField alloc] init];
    [self.view addSubview:_infoTextField];
    [_infoTextField mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(80);
        make.width.mas_equalTo(350);
        make.height.mas_equalTo(20);
    }];
    _infoTextField.placeholder = @"填写真实信息，为你更好的匹配道友";
    _infoTextField.font =[UIFont systemFontOfSize:18];
    
    //iconImage
    _iconImage = [[UIImageView alloc] init];
    [self.view addSubview:_iconImage];
    [_iconImage mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(130);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(120);
    }];
    _iconImage.image = [UIImage imageNamed:@"icon"];
    
    //昵称
    _nickNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 310, 40, 30)];
    [self.view addSubview:_nickNameLabel];
    [_nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make){
           make.left.mas_equalTo(20);
           make.top.mas_equalTo(310);
           make.width.mas_equalTo(40);
           make.height.mas_equalTo(30);
       }];
    _nickNameLabel.text = @"昵称";
    _nickNameLabel.font = [UIFont systemFontOfSize:19];
    
    _nickNaneTextField = [[UITextField alloc] init];
    [self.view addSubview:_nickNaneTextField];
    [_nickNaneTextField mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(90);
        make.top.mas_equalTo(310);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(30);
    }];
    _nickNaneTextField.delegate = self;
    _nickNaneTextField.placeholder = @"输入昵称";
    _nickNaneTextField.font = [UIFont systemFontOfSize:17];
    UIView *nickNanebottomBorder = [[UIView alloc] init];
    [self.view addSubview:nickNanebottomBorder];
    [nickNanebottomBorder mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(341);
        make.width.mas_equalTo(376);
        make.height.mas_equalTo(1);
    }];
    nickNanebottomBorder.backgroundColor = [UIColor colorWithRed:246/255 green:247/255 blue:248/255 alpha:0.3];
    
    
    _femaleIconButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    _femaleIconButton.frame = CGRectMake(280, 310, 55, 29);
    [self.view addSubview:_femaleIconButton];
    [_femaleIconButton mas_makeConstraints:^(MASConstraintMaker *make){
           make.left.mas_equalTo(280);
           make.top.mas_equalTo(310);
           make.width.mas_equalTo(55);
           make.height.mas_equalTo(29);
       }];
    [_femaleIconButton setTitle:@"女" forState:UIControlStateNormal];
    [_femaleIconButton setImage:[UIImage imageNamed:@"female1.png"] forState:UIControlStateNormal];
    [_femaleIconButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_femaleIconButton addTarget:self action:@selector(clickFemaleIcon:) forControlEvents:UIControlEventTouchUpInside];
    _femaleIconButton.titleLabel.font = [UIFont systemFontOfSize:20];
    _femaleIconButton.titleEdgeInsets = UIEdgeInsetsMake(3, 2, 1, 1);
    _femaleIconButton.imageEdgeInsets = UIEdgeInsetsMake(1, 0, 1, 28);
    
    //先设置文字，在加图片，调整大小
    _manIconButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    _manIconButton.frame = CGRectMake(348, 310, 55, 29);
    [self.view addSubview:_manIconButton];
    [_manIconButton mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(348);
        make.top.mas_equalTo(310);
        make.width.mas_equalTo(55);
        make.height.mas_equalTo(29);
    }];
    _manIconButton.tag =1112;
    [_manIconButton setTitle:@"男" forState:UIControlStateNormal];
    //关键要设置字体颜色
    [_manIconButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_manIconButton setImage:[UIImage imageNamed:@"man1.png"] forState:UIControlStateNormal];
    [_manIconButton addTarget:self action:@selector(clickmanIcon:) forControlEvents:UIControlEventTouchUpInside];
    _manIconButton.titleLabel.font = [UIFont systemFontOfSize:20];
    _manIconButton.titleEdgeInsets = UIEdgeInsetsMake(3, 0, 1, 1);
    _manIconButton.imageEdgeInsets = UIEdgeInsetsMake(1, 0, 1, 28);
    
    //出生年月
    _birthdayLable = [[UILabel alloc] init];
    [self.view addSubview:_birthdayLable];
    [_birthdayLable mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(370);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
    }];
    _birthdayLable.text = @"出生年";
    _birthdayLable.font = [UIFont systemFontOfSize:18];
    
    
    _birthdayDataLable = [[UILabel alloc] init];
    [self.view addSubview: _birthdayDataLable];
    [_birthdayDataLable mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(90);
        make.top.mas_equalTo(371);
        make.width.mas_equalTo(350);
        make.height.mas_equalTo(30);
    }];
    _birthdayDataLable.text = @"仅自己可见";
    _birthdayDataLable.textColor = [UIColor grayColor];
    _birthdayDataLable.font = [UIFont systemFontOfSize:17];
     _birthdayDataLable.userInteractionEnabled = YES;
    UITapGestureRecognizer *birthdayDataTag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(birthdayDateLableClick:)];
    [_birthdayDataLable addGestureRecognizer:birthdayDataTag];
    
    UIView *birthdaybottomBorder = [[UIView alloc] init];
    [self.view addSubview:birthdaybottomBorder];
    
    [birthdaybottomBorder mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(402);
        make.width.mas_equalTo(376);
        make.height.mas_equalTo(1);
    }];
    birthdaybottomBorder.backgroundColor = [UIColor colorWithRed:246/255 green:247/255 blue:248/255 alpha:0.3];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)datePickerViewSetBirthdayData:(NSString *)data{
    _birthdayDataLable.text = data;
    _birthdayDataLable.textColor = [UIColor blackColor];
    _birthdayData = data;
    [self checkNextButtonIsClick];
}

- (void)birthdayDateLableClick: (UITapGestureRecognizer*) lable{
    DatePickerView *datePickerView = [[DatePickerView alloc] init];
    [datePickerView showView:[self getKeyWindow]];
    [datePickerView mas_makeConstraints:^(MASConstraintMaker *make){
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(SCREEN_HEIGHT);
    }];
    datePickerView.delegate = self;
}


- (void)clickFemaleIcon:(UIButton*)button{
    if (_sex == GenderTypeUndefine || _sex == GenderTypeMan) {
        [button setImage:[UIImage imageNamed:@"female"] forState:UIControlStateNormal];
    }
    if (_sex == GenderTypeMan) {
        [_manIconButton setImage:[UIImage imageNamed:@"man1"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"female"] forState:UIControlStateNormal];
    }
    _sex = GenderTypeFemale;
    [self checkNextButtonIsClick];
}

- (void)clickmanIcon:(UIButton*)button{
    if (_sex == GenderTypeUndefine || _sex == GenderTypeMan) {
        [button setImage:[UIImage imageNamed:@"man"] forState:UIControlStateNormal];
    }
    if (_sex == GenderTypeFemale) {
        [_femaleIconButton setImage:[UIImage imageNamed:@"female1"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"man"] forState:UIControlStateNormal];
    }
    _sex = GenderTypeMan;
    [self checkNextButtonIsClick];
}

- (void)backButtonDidPress{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)textFieldDidChangeSelection:(UITextField *)textField{
    [self checkNextButtonIsClick];
}

- (void)checkNextButtonIsClick{
    if (_nickNaneTextField.text.length > 0  && [_birthdayData length] > 0 && _sex != GenderTypeUndefine ) {
        _rightItem.enabled = YES;
        _nextButton.backgroundColor = [UIColor blueColor];
    }else{
        _rightItem.enabled = NO;
        _nextButton.backgroundColor = [UIColor colorWithRed:239/255 green:241/255 blue:242/255 alpha:0.3];
    }
}

- (void)nextButtonDidPress{
    [self setInfromation];
    TabBarViewController *tabBarVC = [[TabBarViewController alloc] init];
    [self getKeyWindow].rootViewController = tabBarVC;
}

- (void)setInfromation{
    UserSingleton *user = [UserSingleton new];
    user.model = [[InformationModel alloc] init];
    user.model.nickName = _nickNaneTextField.text;
    user.model.birthday = _birthdayDataLable.text;
    user.model.gender = _sex;
    user.model.describe = _nickNaneTextField.text;
    [user.model writeData];
    
    InformationModel * model = [InformationModel readData];
    NSLog(@"%@",model.nickName);
}

- (UIWindow*)getKeyWindow{
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        if (window.isKeyWindow) {
            return window;
        }
    }
    return nil;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
